﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

// stsadm -o setpolicyschedule -schedule "every 1 minutes between 0 and 59"

namespace Behemoth.SharePoint.TimerJobs {

    /// <summary>
    /// IMPORTANT!!! For development servers only!!!
    /// Provides methods for qwuickly chnaging the Policy timer
    /// job so it runs more (or less) often.
    /// </summary>
    public class DevelopmentPolicyScheduler {

        static Guid servicesId = new Guid("675d6a05-c321-414b-bd6e-ee936540c719");

        public const string FAST_SCHEDULE = "every 5 minutes between 0 and 59";
        public const string MED_SCHEDULE = "hourly between 0 and 59";
        public const string SLOW_SCHEDULE = "daily between 03:00:00 and 05:30:00";
        public const string PolicyServiceName = "PolicyConfigService";
        public const string PolicyJobName = "Information management policy";

        public static void SetFast() {
            SetSchedule(PolicyServiceName, PolicyJobName, FAST_SCHEDULE);
        }
        public static void SetFast(string serviceName, string jobName) {
            SetSchedule(serviceName, jobName, FAST_SCHEDULE);
        }

        public static void SetMedium() {
            SetSchedule(PolicyServiceName, PolicyJobName, MED_SCHEDULE);
        }
        public static void SetMedium(string serviceName, string jobName) {
            SetSchedule(serviceName, jobName, MED_SCHEDULE);
        }

        public static void SetSlow() {
            SetSchedule(PolicyServiceName, PolicyJobName, SLOW_SCHEDULE);
        }
        public static void SetSlow(string serviceName, string jobName) {
            SetSchedule(serviceName, jobName, SLOW_SCHEDULE);
        }

        // An awesome resource for setting schedules:
        // http://blogs.msdn.com/markarend/archive/2007/01/16/spschedule-fromstring-recurrencevalue-syntax-format-for-recurrence-value.aspx
        // thanks to http://blah.winsmarts.com/2007-6-Developer_tip_-_Testing_SharePoint_timer_jobs_in_a_development_environment.aspx
        // for helping me fix this code, which was broken because it relied in farn specific GUIDs,
        public static void SetSchedule(string serviceName, string jobName, string scheduleText) {
            //SPService service = SPFarm.Local.Services[serviceName]; // !!this does not work!!
            SPService service = null;
            foreach (SPService s in SPFarm.Local.Services) {
                if (string.Compare(serviceName, s.Name, true) == 0) {
                    service = s;
                    break;
                }
            }
            if (service == null)
                throw new ArgumentException(string.Format("There was no service with name '{0}' on the local farm.", serviceName), "serviceName");
            foreach (SPJobDefinition job in service.JobDefinitions) {
                if (string.IsNullOrEmpty(jobName) || string.Compare(jobName, job.Title, true) == 0) {
                    job.Schedule = SPSchedule.FromString(scheduleText);
                    job.Update();
                }
            }
        }

    } // class

} // namespace